package com.jdrx.sw.sewage.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.entity.DictPO;
import com.jdrx.sw.sewage.beans.vo.ExcelBaImGreDevVO;
import com.jdrx.sw.sewage.common.converter.LocalDateConverter;
import com.jdrx.sw.sewage.common.util.SpringBeanUtil;
import com.jdrx.sw.sewage.common.util.StringUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;

import static com.jdrx.sw.sewage.common.constant.Constant.*;

@Service
public class GDDataCheckServiceImpl extends AnalysisEventListener<ExcelBaImGreDevVO> {

    public static final Logger logger = LoggerFactory.getLogger(GDDataCheckServiceImpl.class);
    //    List<String> listP = new ArrayList<String>();
//    List<String> listD = new ArrayList<String>();
//    List<String> listPi = new ArrayList<String>();
    HashSet<String> listP = new HashSet();
    HashSet<String> listD = new HashSet();
    HashSet<String> listPi = new HashSet();
    HashMap map = new HashMap();

    @Autowired
    private SpringBeanUtil springBeanUtil;
    @Autowired
    private DictServiceImpl dictServiceImpl;
    @Autowired
    private HttpServletRequest request;

    /**
     * 这个每一条数据解析都会来调用
     */
    @SneakyThrows
    @Override
    public void invoke(ExcelBaImGreDevVO data, AnalysisContext context) {
        logger.info("解析到一条数据:{}", JSON.toJSONString(data));
        //校验工艺单元
        Map<String, Object> params = Maps.newHashMap();

        String processunit = StringUtil.removeStrMatcher(data.getProcessunit());
        String gatherDev = data.getGatherDev();
        String gatherDevPoint = data.getGatherDevPoint();
        if (gatherDev.contains(NH3NY)) gatherDev = NH3NY;
        if (gatherDev.contains(H2SY)) gatherDev = H2SY;
        if (gatherDev.contains(BOD5Y)) gatherDev = BOD5Y;
        if (gatherDevPoint.contains(NH3N)) gatherDevPoint = NH3N;
        if (gatherDevPoint.contains(H2S)) gatherDevPoint = H2S;
        if (gatherDevPoint.contains(BOD5)) gatherDevPoint = BOD5;

        if (!gatherDev.contains(NH3NY) && !gatherDev.contains(BOD5Y) && !gatherDev.contains(H2SY))
            gatherDev = StringUtil.removeStrMatcher(gatherDev);
        if (!gatherDevPoint.contains(NH3N) && !gatherDevPoint.contains(BOD5) && !gatherDevPoint.contains(H2S))
            gatherDevPoint = StringUtil.removeStrMatcher(gatherDevPoint);

        params.put(CLASSIFYKEY, PROCESSUNITCLASSIFY);
        params.put(VALUE, processunit);
        DictPO dictPO = dictServiceImpl.getByParams(params, null);
        if (Objects.isNull(dictPO)) listP.add(processunit);

        //校验设备
        params.put(CLASSIFYKEY, GATRDEVCLASSIFY);
        params.put(VALUE, gatherDev);
        dictPO = dictServiceImpl.getByParams(params, null);
        if (Objects.isNull(dictPO)) listD.add(gatherDev);

        //校验点位
        String d_change = D_CHANGE.get(data.getGatherDevPoint());
        if (!StringUtils.isEmpty(d_change)) gatherDevPoint = d_change;
        params.put(CLASSIFYKEY, GATRDEVPOINTCLASSIFY);
        params.put(VALUE, gatherDevPoint);
        dictPO = dictServiceImpl.getByParams(params, null);
        if (Objects.isNull(dictPO)) listPi.add(gatherDevPoint);
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        map.put("工艺单元", listP);
        map.put("采集名称", listD);
        map.put("点位", listPi);
        logger.info("所有数据导入完成！");
    }

    public ResponseVO collectionDataCheck(MultipartFile file) throws IOException {
        map.clear();
        listP.clear();
        listD.clear();
        listPi.clear();

        EasyExcel.read(file.getInputStream(), ExcelBaImGreDevVO.class, SpringBeanUtil.getBean(GDDataCheckServiceImpl.class))
                .registerConverter(new LocalDateConverter())
                .sheet()
                .doRead();

        if (listP.size() <= 0 && listD.size() <= 0 && listPi.size() <= 0) return ResponseFactory2.ok("校验通过");
        return ResponseFactory2.err(map);
    }
}